package lumingweihua.future.cn.lumingweihua.ui.mine;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.SimpleHttpListener;
import lumingweihua.future.cn.lumingweihua.network.SimpleTextWatcher;
import lumingweihua.future.cn.lumingweihua.network.UserManager;
import lumingweihua.future.cn.lumingweihua.ui.BaseCompatActivity;

/**
 * todo 修改昵称
 * Created by z1986 on 2018/3/17.
 */

public class ModifyNameActivity extends BaseCompatActivity {
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;

    LoadData<String> modifyData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_modify);
        ButterKnife.bind(this);
        init();
    }

    public void init() {
        modifyData = new LoadData<>(LoadData.Api.个人中心头像昵称修改, this);
        modifyData._setOnLoadingListener(new SimpleHttpListener<String>() {
            @Override
            public void onLoadComplete(LoadData.Api api, HttpRequest<Object> httpRequest, IHttpResult<String> result) {
                showToast(result.getData());
                UserManager.getInstance().nickname = editName.getText().toString();
                setResult(RESULT_OK);
                finish();
            }
        });
        editName.setText(UserManager.getInstance().nickname);
        editName.addTextChangedListener(new SimpleTextWatcher(tvSubmit));
    }

    @OnClick({R.id.tv_submit})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_submit:
                String name = editName.getText().toString();
                if (name.length() > 6) {
                    _showToast("昵称最多6位");
                    return;
                }
                modifyData._loadData("nickname", name);
                break;
        }
    }
}
